<style lang="less" scoped>
    @import './list.less';
</style>
<template>
    <div>
        <Row>
            <Col span="24">
                <Card style="margin-bottom: 10px">
                    <Form inline>
                        <FormItem style="margin-bottom: 0">
                            <Select v-model="searchConf.status" clearable placeholder='请选择状态' style="width:100px">
                                <Option :value="1">启用</Option>
                                <Option :value="0">禁用</Option>
                            </Select>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Select v-model="searchConf.type" clearable placeholder="请选择类别" style="width:100px">
                                <Option :value="1">接口标识</Option>
                                <Option :value="2">接口名称</Option>
                                <Option :value="3">真实类库</Option>
                            </Select>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Input v-model="searchConf.keywords" placeholder=""></Input>
                        </FormItem>
                        <FormItem style="margin-bottom: 0">
                            <Button type="primary" @click="search">搜索/刷新</Button>
                        </FormItem>
                    </Form>
                </Card>
            </Col>
        </Row>
        <Row>
            <Col span="24">
                <Card>
                    <p slot="title" style="height: 32px">
                        <Button type="primary" @click="alertAdd" icon="md-add">新增</Button>
                        <Button type="warning" class="margin-left-5" @click="confirmRefresh = true" icon="md-refresh" style="margin: 0 5px">
                            刷新路由
                        </Button>
                        <Button type="info" class="margin-left-5" to="/wiki/list" icon="md-bookmarks">接口文档</Button>
                    </p>
                    <div>
                        <Table :loading="tabLoading" :columns="columnsList" :data="tableData" border disabled-hover>
                            <!--状态-->
                            <template slot-scope="{ row, index }" slot="status">
                                <i-switch v-model="row.status" :true-value="1" :false-value="0"
                                          @on-change="statusSwitch(row)"/>
                            </template>
                            <!--操作-->
                            <template slot-scope="{ row, index }" slot="option">
                                <!--请求方式-->
                                <Button type="info" style="margin-right: 5px"
                                        @click="()=>{$router.push({name: 'interface_request',query: { hash: row.hash }})}">
                                    请求方式
                                </Button>
                                <!--返回参数-->
                                <Button type="warning" style="margin-right: 5px"
                                        @click="()=>{$router.push({name: 'interface_response',query: { hash: row.hash }})}">
                                    返回参数
                                </Button>
                                <!--编辑-->
                                <Button type="primary" style="margin-right: 5px" @click="editShow(row,index)">编辑
                                </Button>
                                <!--删除-->
                                <Poptip word-wrap confirm transfer title="您确定要删除这条数据吗?" @on-ok="Del(row,index)">
                                    <Button type="error" :loading="row.loading">
                                        <span v-if="!row.loading">删除</span>
                                        <span v-else>删除</span>
                                    </Button>
                                </Poptip>
                            </template>
                        </Table>
                    </div>
                    <div class="margin-top-15" style="text-align: center">
                        <Page :total="tableShow.listCount" :current="tableShow.currentPage"
                              :page-size="tableShow.pageSize" @on-change="changePage"
                              @on-page-size-change="changeSize" show-elevator show-sizer show-total></Page>
                    </div>
                </Card>
            </Col>
        </Row>
        <Modal v-model="modalSetting.show" width="668" :styles="{top: '30px'}" @on-visible-change="doCancel">
            <p slot="header" style="color:#2d8cf0">
                <Icon type="md-alert"></Icon>
                <span>{{formItem.id ? '编辑' : '新增'}}接口</span>
            </p>
            <Form ref="myForm" :rules="ruleValidate" :model="formItem" :label-width="90">
                <FormItem label="接口名称" prop="info">
                    <Input v-model="formItem.info" placeholder="请输入接口名称"></Input>
                </FormItem>
                <FormItem label="真实类库" prop="api_class">
                    <Input v-model="formItem.api_class" placeholder="请输入真实类库"></Input>
                </FormItem>
                <FormItem label="接口分组" prop="group_hash">
                    <Select v-model="formItem.group_hash" style="width:200px">
                        <Option v-for="(v, i) in apiGroup" :value="v.hash" :kk="i" :key="v.hash"> {{v.name}}</Option>
                    </Select>
                </FormItem>
                <FormItem label="请求方式" prop="method">
                    <Select v-model="formItem.method" style="width:200px">
                        <Option :value="0" :key="0"> 不限</Option>
                        <Option :value="1" :key="1"> POST</Option>
                        <Option :value="2" :key="2"> GET</Option>
                    </Select>
                </FormItem>
                <FormItem label="接口映射" prop="hash">
                    <Input style="width: 300px" disabled v-model="formItem.hash"></Input>
                    <Tag color="error" class="margin-left-5">系统自动生成，不允许修改</Tag>
                </FormItem>
                <FormItem label="AccessToken" prop="access_token">
                    <Select v-model="formItem.access_token" style="width:200px">
                        <Option :value="0" :key="0"> 简易认证</Option>
                        <Option :value="1" :key="1"> 复杂认证</Option>
                    </Select>
                    <Tooltip placement="right" max-width="800">
                        <Icon type="md-help-circle" class="margin-left-5" color="#2d8cf0" size="20"/>
                        <div slot="content">
                            <p>新版本的全部接口都必须在Header中传递access-token字段，所以AccessToken必须要验证。</p>
                            <p>简易认证：在请求这类接口时候，请直接在header中传递AppSecret即可，终身有效。</p>
                            <p>复杂认证：在请求这类接口时候，先请求getAccessToken接口获取可用的AccessToken，记住这里的AccessToken默认只有2小时的有效期。</p>
                        </div>
                    </Tooltip>
                </FormItem>
                <FormItem label="测试模式" prop="is_test">
                    <Select v-model="formItem.is_test" style="width:200px">
                        <Option :value="0" :key="0"> 生产模式</Option>
                        <Option :value="1" :key="1"> 测试模式</Option>
                    </Select>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel" style="margin-right: 8px">取消</Button>
                <Button type="primary" @click="submit" :loading="modalSetting.loading">确定</Button>
            </div>
        </Modal>
        <Modal v-model="confirmRefresh" width="360">
            <p slot="header" style="color:#f60;text-align:center">
                <Icon type="information-circled"></Icon>
                <span>确定要刷新路由么</span>
            </p>
            <div style="text-align:center">
                <p>刷新路由是一个非常危险的操作，它有可能影响到您现有接口的访问，请确认无误后刷新！！</p>
            </div>
            <div slot="footer">
                <Button type="error" size="large" long :loading="refreshLoading" @click="refreshRoute">确定刷新</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
    import {getList, changeStatus, add, edit, del, getHash, refresh} from '@/api/interface'
    import {getAll} from '@/api/interface-group'

    export default {
        name: 'interface_list',
        data() {
            return {
                confirmRefresh: false,
                refreshLoading: false,
                columnsList: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 65,
                        align: 'center'
                    },
                    {
                        title: '接口名称',
                        align: 'center',
                        minWidth: 190,
                        key: 'info'
                    },
                    {
                        title: '真实类库',
                        align: 'center',
                        key: 'api_class',
                        width: 190
                    },
                    {
                        title: '接口映射',
                        align: 'center',
                        key: 'hash',
                        width: 140
                    },
                    {
                        title: '请求方式',
                        align: 'center',
                        width: 95,
                        render: (h, params) => {
                            if (params.row.isTest === 1) {
                                return h('tag', {
                                    attrs: {
                                        color: 'error'
                                    }
                                }, '测试')
                            } else {
                                switch (params.row.method) {
                                    case 1:
                                        return h('Tag', {
                                            attrs: {
                                                color: 'success'
                                            }
                                        }, 'POST')
                                    case 2:
                                        return h('Tag', {
                                            attrs: {
                                                color: 'primary'
                                            }
                                        }, 'GET')
                                    case 0:
                                        return h('Tag', {
                                            attrs: {
                                                color: 'warning'
                                            }
                                        }, '不限')
                                }
                            }
                        }
                    },
                    {
                        title: '接口状态',
                        align: 'center',
                        width: 130,
                        slot: 'status'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        minWidth: 375,
                        slot: 'option'
                    }
                ],
                tableData: [],
                apiGroup: [],
                tableShow: {
                    currentPage: 1,
                    pageSize: 10,
                    listCount: 0
                },
                searchConf: {
                    type: '',
                    keywords: '',
                    status: ''
                },
                modalSetting: {
                    show: false,
                    loading: false,
                    index: 0
                },
                formItem: {
                    api_class: '',
                    info: '',
                    group_hash: 'default',
                    method: 2,
                    hash: '',
                    access_token: 0,
                    is_test: 0,
                    id: 0
                },
                ruleValidate: {
                    api_class: [
                        {required: true, message: '真实类库不能为空', trigger: 'blur'}
                    ],
                    info: [
                        {required: true, message: '接口名称不能为空', trigger: 'blur'}
                    ]
                },
                tabLoading: false // 加载表格数据
            }
        },
        created() {
            this.getList()
            this.getApiGroup()
        },
        methods: {
            getApiGroup(){
                let vm = this
                getAll().then(response => {
                    vm.apiGroup = response.data.data.list
                })
            },
            alertAdd() {
                let vm = this
                getHash().then(response => {
                    vm.formItem.hash = response.data.data.hash
                })
                vm.modalSetting.show = true
            },
            submit() {
                let vm = this
                this.$refs['myForm'].validate((valid) => {
                    if (valid) {
                        vm.modalSetting.loading = true
                        if (vm.formItem.id === 0) {
                            add(vm.formItem).then(response => {
                                vm.modalSetting.loading = false
                                vm.$Message.success(response.data.msg)
                                vm.getList()
                                vm.cancel()
                            }).catch(() => {
                                vm.modalSetting.loading = false
                            })
                        } else {
                            edit(vm.formItem).then(response => {
                                vm.modalSetting.loading = false
                                vm.$Message.success(response.data.msg)
                                vm.getList()
                                vm.cancel()
                            }).catch(() => {
                                vm.modalSetting.loading = false
                            })
                        }
                    }
                })
            },
            cancel() {
                this.modalSetting.show = false
            },
            changePage(page) {
                this.tableShow.currentPage = page
                this.getList()
            },
            changeSize(size) {
                this.tableShow.pageSize = size
                this.getList()
            },
            search() {
                this.tableShow.currentPage = 1
                this.getList()
            },
            getList() {
                let vm = this
                this.tabLoading = true
                getList({
                    page: vm.tableShow.currentPage,
                    size: vm.tableShow.pageSize,
                    type: vm.searchConf.type,
                    keywords: vm.searchConf.keywords,
                    status: vm.searchConf.status
                }).then(response => {
                    this.tabLoading = false
                    for (let v of response.data.data.list) {
                        v.loading = false
                    }
                    vm.tableData = response.data.data.list
                    vm.tableShow.listCount = response.data.data.count
                }).catch(() => {
                    this.tabLoading = false
                })
            },
            doCancel(data) {
                if (!data) {
                    this.formItem.id = 0
                    this.$refs['myForm'].resetFields()
                    this.modalSetting.loading = false
                    this.modalSetting.index = 0
                }
            },
            refreshRoute() {
                let vm = this
                vm.refreshLoading = true
                refresh().then(response => {
                    vm.$Message.success(response.data.msg)
                    vm.confirmRefresh = false
                    vm.refreshLoading = false
                }).catch(() => {
                    vm.confirmRefresh = false
                    vm.refreshLoading = false
                })
            },
            // 状态切换
            statusSwitch(row) {
                changeStatus(row.status, row.hash).then(response => {
                    this.$Message.success(response.data.msg)
                    this.getList()
                })
            },
            // 显示编辑model
            editShow(row, index) {
                let vm = this
                vm.formItem.id = row.id
                vm.formItem.api_class = row.api_class
                vm.formItem.info = row.info
                vm.formItem.method = row.method
                vm.formItem.hash = row.hash
                vm.formItem.group_hash = row.group_hash
                vm.formItem.access_token = row.access_token
                vm.formItem.is_test = row.is_test
                vm.formItem.need_login = row.need_login
                this.modalSetting.show = true
                this.modalSetting.index = index
            },
            // 删除
            Del(row, index) {
                row.loading = true
                del(row.hash).then(response => {
                    row.loading = false
                    this.tableData.splice(index, 1)
                    this.$Message.success(response.data.msg)
                }).catch(() => {
                    row.loading = false
                })
            }
        }
    }
</script>
